package com.anxin.act.task.complete;

import com.alibaba.fastjson.JSONArray;
import com.anxin.act.task.complete.impl.FreeFlowCompleteImpl;
import com.anxin.act.task.vo.TaskVo;
import com.anxin.act.utils.ActProcessUtils;
import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.StringUtils;
import org.activiti.engine.impl.bpmn.behavior.ParallelMultiInstanceBehavior;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提交任务
 * @author: liuxiangyu
 * @date: 2020/4/26 16:21
 */
@Service
public class TaskCompleteFactory {

    private Map<String,Object> implMap = new HashMap<>();

    @Autowired
    private ApplicationContext context;


    /**
     * 待办提交类型
     * task:正常提交
     * addBefore:前加签提交
     * addAfter:后加签提交
     * reading:阅办提交
     * freeFlow:自由流提交
     * entrust:转签
     */
    public TaskCompleteFactory(){
        // 正常提交
        implMap.put("task","actTaskCompleteImpl");
        // 后加签
        implMap.put("addAfter","addAfterCompleteImpl"); // 已改最新
        // 前加签
        implMap.put("addBefore","addBeforeCompleteImpl"); // 已改为最新
        // 转签
        implMap.put("entrust","entrustCompleteImpl"); // 已改最新
        // 自由流
        implMap.put("freeFlow","freeFlowCompleteImpl");
        // 自建流程
        implMap.put("selfBuilt","selfBuiltCompleteImpl");
        // 传阅
        implMap.put("reading","readingCompleteImpl");
        // 并行前加签提交
        // implMap.put("beforeTask","beforeTaskCompleteImpl");
        // 驳回
        implMap.put("rejected","rejectedCompleteImpl");
        // 被驳回的节点提交
        implMap.put("rejectedJump","rejectedJumpImpl");
        // 新-前加签任务提交
        implMap.put("addBeforeTask","addBeforeTaskCompleteImpl");
        // 新-后加签任务提交
        implMap.put("addAfterTask","addAfterTaskCompleteImpl");
        // 新-转签加签任务提交
        implMap.put("entrustTask","entrustTaskCompleteImpl");
        // 新-申请人补充材料
        implMap.put("applicantSupplement","applicantSupplementCompleteImpl");

    }

    /**
     * 根据类型获取实现类
     * @param taskVo
     * @return
     */
    public TaskComplete get(TaskVo taskVo){
        // 提交方式
        String completeType = taskVo.getCompleteType();


        // actTask为actitivi的待办，如果不是则是系统加签出来的待办
        if ("task".equals(completeType) && !"actTask".equals(taskVo.getTaskType())){
            completeType = taskVo.getTaskType() + "Task";
        }


        // 被驳回的节点提交
        if (taskVo.getTask() != null) {
            Object rejectedCompleteType = taskVo.getTask().getTaskLocalVariables().get("rejectedCompleteType");
            if (rejectedCompleteType != null && !"normal".equals(rejectedCompleteType)) {
                completeType = "rejectedJump";
            }
        }


        if ("申请人补充材料".equals(taskVo.getFormFieldValue("approved"))){
            completeType = "applicantSupplement";
            JSONArray addUser = new JSONArray();
            addUser.add(ActProcessUtils.getProcessApplicant(taskVo.getActId()));
            taskVo.setAddUser(addUser);
        }

        Map<String, TaskComplete> completeImplMap = context.getBeansOfType(TaskComplete.class);
        return completeImplMap.get(implMap.get(completeType));
    }

}
